package com.cory.admin.sys.controller.portal;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.beust.jcommander.internal.Lists;
import com.cory.admin.sys.config.AdminConstants;
import com.cory.admin.sys.controller.AdminController;
import com.cory.admin.sys.domain.entity.SysResource;
import com.cory.admin.sys.domain.portal.MsgVO;
import com.cory.boot.domain.bean.Result;
import com.cory.boot.utils.SpringUtils;
import com.cory.module.msg.admin.AdminMsg;
import com.cory.module.msg.admin.AdminMsgClient;
import com.cory.module.msg.admin.AdminMsgService;
import com.cory.module.msg.admin.annotation.AdminMsgSettings;
import com.cory.utils.lang.StringUtils;
import com.google.common.collect.ImmutableMap;

/**
 * @author xiongcong
 * @date 2022-09-17
 */
@Controller
@RequestMapping("portal/msg")
public class MsgController extends AdminController {
    
    @Autowired
    private AdminMsgService adminMsgService;
    
    @ResponseBody
    @RequestMapping("list")
    @SuppressWarnings("unchecked")
    public Result<?> list(HttpSession session) {
        
        List<SysResource> resources = (List<SysResource>)session.getAttribute(AdminConstants.SESSION_USER_RESOURCE);
        Map<String, SysResource> resourceMaps = resources.stream().filter(i -> StringUtils.isNotBlank(i.getCode()))
                .collect(Collectors.toMap(i -> i.getCode(), i -> i));
        List<String> permissions = (List<String>)session.getAttribute(AdminConstants.SESSION_USER_PERMISSION);

        List<AdminMsg> msgs = adminMsgService.getMyAdminMsgs();
        
        List<MsgVO> list = Lists.newArrayList();
        for (AdminMsg msg : msgs) {
            AdminMsgSettings settings = msg.settings();
            
            if (msg.getMsg() <= 0) {
                continue;
            }
            
            if (StringUtils.isBlank(settings.menuCode())) {
                continue;
            }
            
            SysResource menu = resourceMaps.get(settings.menuCode());
            if (menu == null) {
                continue;
            }
            
            if (StringUtils.isNotBlank(settings.permission()) && !permissions.contains(settings.permission())) {
                continue;
            }
            
            String text = String.format("%s：有 %d 条消息待处理", StringUtils.defaultIfBlank(settings.name(), menu.getName()), msg.getMsg());
            String url = String.format("/portal/menu/link?menuId=%d", menu.getId());
            MsgVO vo = new MsgVO(menu.getName(), msg.getMsg(), text, url);
            list.add(vo);
        }
        
        long msgCount = list.stream().map(i -> i.getCount()).reduce(0L, Long::sum);
        return Result.success("ok", ImmutableMap.of("msgs", msgCount, "list", list));
    }
    
    @ResponseBody
    @RequestMapping("init")
    public String init() {
        AdminMsgClient.init();
        SpringUtils.getBean(AdminMsgService.class).initPlatformAdminMsg();
        return "ok";
    }
}
